<script>
	import app_js from "@/utils/app.js";
	import storage from "./utils/storage";
	import './eventCore/index.js';


	// #ifdef APP-PLUS
	import {
		registerRequestPermissionTipsListener,
		unregisterRequestPermissionTipsListener,
		setRequestPermissionTips
	} from "@/uni_modules/uni-registerRequestPermissionTips"
	const PermissionTips = {
		"android.permission.CAMERA": "<h4 style=\"font-size:40px;\">正在获取您的摄像头权限</h4><font color=#cccccc>为了获取您的头像、背景、参考图片</font>",
		"android.permission.READ_EXTERNAL_STORAGE": "<h4 style=\"font-size:40px;\">正在获取您的相册读取权限</h4><font color=#cccccc>为了获取您的头像、背景、参考图片</font>",
		"android.permission.WRITE_EXTERNAL_STORAGE": "<h4 style=\"font-size:40px;\">正在获取您的相册写入权限</h4><font color=#cccccc>为了保存您生成的图片、视频</font>",
	}
	// #endif

	export default {
		onLaunch: function() {
			console.log('App Launch');


			//获取设备信息并保存到缓存
			uni.getSystemInfo({
				success: function(data) {
					const {
						statusBarHeight,
						screenWidth,
						screenHeight
					} = data;
					storage.setCache('statusBarHeight', statusBarHeight);
					storage.setCache('screenWidth', screenWidth);
					storage.setCache('screenHeight', screenHeight);
				},
			});


			// #ifdef APP-PLUS
			if (plus.os.name !== "iOS") {
				var brand = uni.getSystemInfoSync().deviceBrand;
				setRequestPermissionTips(PermissionTips)
				registerRequestPermissionTipsListener({
					onRequest: (e) => {
						console.log(e)
					},
					onConfirm: (e) => {
						console.log(e)
					},
					onComplete: (e) => {
						// 华为手机在权限禁止之后，再次申请权限不会出现权限申请框。此时应该引导用户去系统设置开启此权限，不应该频繁申请。
						if (brand.toLowerCase() == "huawei") {
							let tips = {}
							let hasDeniedPermission = false
							for (let k in PermissionTips) {
								if (e[k] != "denied") {
									tips[k] = PermissionTips[k]
								} else {
									hasDeniedPermission = true
								}
							}
							setRequestPermissionTips(tips) // 更新弹框提醒，防止华为手机不出现权限申请框时权限提醒框闪烁的情况
							// if (hasDeniedPermission)
							// 	uni.showModal({
							// 		content: "权限已经被拒绝，请前往设置中开启"
							// 	})
						}
					}
				})
			}
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		// #ifdef APP-PLUS
		onExit: function() {
			if (plus.os.name !== "iOS") {
				unregisterRequestPermissionTipsListener(null)
			}
		},
		// #endif
		globalData: app_js
	}
</script>

<style lang="less">
	/*每个页面公共css */
	@import "@/styles/index.less";
</style>